class Window_Factory < Window_Selectable
  attr_accessor   :make_window
  attr_accessor   :tables
  attr_accessor   :selected_recipe
  attr_accessor   :message_window
  def initialize(tables)
    super(0, 64, 640, 416)
    # Make help window, item window
    @help_window = Window_Help.new
    @message_window = Window_Message.new
    # Associate help window
    # Make target window (set to invisible / inactive)
    @column_max = 2
    @tables = tables
    @selected_recipe = @tables[0]
    self.index = 0
    # If in battle, move window to center of screen
    # and make it semi-transparent
    @item_max = tables.size
    re_draw
  end
  def re_draw
    self.contents = Bitmap.new(width - 32, row_max * 32)
    indexVal = 0
    @tables.each do |item|
      if item
        draw_item(item, indexVal)
        indexVal += 1
      end
    end
  end
  def update
    if @message_window != nil && @message_window.active
      @message_window.update
      return true
    end
    $game_temp.message_window_showing = false
    @message_window.visible = false
    if @make_window && @make_window.active
      @make_window.update
      if Input.trigger?(Input::C)
        if @selected_recipe.make_able_count > 0
          self.do_make_item
          self.close_make_confirm
        end
      end
      if Input.trigger?(Input::B)
        self.close_make_confirm
      end
      return true
    else
      super
      @selected_recipe = @tables[@index]
      if Input.trigger?(Input::C)
        self.open_make_confirm
      end
      if Input.trigger?(Input::B)
        GlobalMethods.addLog('Input::B')
        return false
      end
    end
    return true
  end
  def open_make_confirm
    text_list = Array.new
    if @selected_recipe.make_able_count > 0
      text_list.push('制作')
    end
    @selected_recipe.list.each do |item|
      text_list.push('需要：' + item.item.name + 'x' + item.need_number.to_s) if item
    end
    @make_window = Window_Command.new(192, text_list)
    @make_window.x = 320
    @make_window.y = 240
    @make_window.active = true
    # Execute transition
    Graphics.transition
  end
  def close_make_confirm
    @make_window.active = false
    @make_window.dispose
    @make_window = nil
  end
  def do_make_item
    @selected_recipe.list.each do |item|
      $game_party.lose_item(item.item.id, item.need_number) if item
    end
    $game_party.gain_item(@selected_recipe.result.id, 1)
    $game_temp.message_text = '制作了' + @selected_recipe.result.name + 'x1'
    $game_temp.message_window_showing = true
    @message_window.active = true
    @message_window.visible = true
    @tables.each do |item|
      item.check_make_able_count
    end
    self.re_draw
  end
  def dispose
    super
    @active = false
    @help_window.dispose
  end
  def update_help
    @help_window.set_text(@selected_recipe.result.description)
  end
  def draw_item(item, index)
    if item.make_able_count > 0
      self.contents.font.color = normal_color
    else
      self.contents.font.color = disabled_color
    end
    x = 4 + index % 2 * (288 + 32)
    y = index / 2 * 32
    rect = Rect.new(x, y, self.width / @column_max - 32, 32)
    self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
    bitmap = RPG::Cache.icon(item.result.icon_name)
    opacity = self.contents.font.color == normal_color ? 255 : 128
    self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
    self.contents.draw_text(x + 28, y, 204, 32, item.result.name, 0)
    self.contents.draw_text(x + 232, y, 48, 32, item.make_able_count.to_s, 2)
  end
end
